package com.yc.ycpaimon.ui

import com.yc.jetpacklib.net.doSuccess
import com.yc.jetpacklib.widget.dialog.YcCommonDialog
import com.yc.ycpaimon.base.BaseActivity
import com.yc.ycpaimon.data.entity.VersionEntity
import com.yc.ycpaimon.databinding.WelcomeActivityBinding
import com.yc.ycpaimon.notification.NotificationUtil
import com.yc.ycpaimon.ui.main.MainActivity
import com.yc.ycpaimon.viewmodel.WelcomeVM
import com.yc.ycpaimon.widget.VersionUpgradeDialog

/**
 * Creator: yc
 * Date: 2022/2/17 17:11
 * UseDes:欢迎页面
 */

class WelcomeActivity : BaseActivity<WelcomeActivityBinding>(WelcomeActivityBinding::inflate) {
    private val mWelcomeVM: WelcomeVM by ycViewModels()
    private val mDialog: VersionUpgradeDialog by lazy {
        VersionUpgradeDialog(this).apply {
            setCancel { toNext() }
        }
    }

    override fun WelcomeActivityBinding.initView() {
        if (!NotificationUtil.checkPermission()) {
            YcCommonDialog(this@WelcomeActivity, this@WelcomeActivity).setMsg("通知权限未开启").setSingleBtnText("开启").setSingleOnClick {
                NotificationUtil.openPush(this@WelcomeActivity)
            }.showSingle()
        }
        mWelcomeVM.versionUpdate().observe {
            it.doSuccess {
                versionUpdate(it)
            }.ycShowNetError()
        }
    }

    private fun versionUpdate(versionData: VersionEntity) {
        if (versionData.isUpdate) {
            mDialog.setData(versionData).show()
        } else {
            toNext()
        }
    }

    private fun toNext() {
        BbsWebViewActivity.newInstance(this)
//        MainActivity.newInstance(this)
    }
}